#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<queue>
using namespace std;

int bfs(vector<vector<char>>& v, int a, int b);

int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
{
    int row = entrance.front();
    int col = entrance.back();
    int ret = bfs(maze, row, col);
    return ret;
}


int bfs(vector<vector<char>>& v, int a, int b)
{
    int dx[4] = { 0,0,-1,1 };
    int dy[4] = { 1,-1,0,0 };
    queue<pair<int, int>> q;
    vector<vector<bool>> visited(v.size(), vector<bool>(v[0].size(), false));
    int count = 0;
    q.push({ a,b });
    visited[a][b] = true;
    while (q.size())
    {
        int sz = q.size();
        count++;
        for (int i = 0; i < sz; i++)
        {
            int m = q.front().first;
            int n = q.front().second;
            q.pop();
            for (int j = 0; j < 4; j++)
            {
                int x = m + dx[j];
                int y = n + dy[j];
                if (x >= 0 && y >= 0 && x < v.size() && y < v[0].size() && v[x][y] == '.' && visited[x][y] == false)
                {
                    if (x == 0 || y == 0 || x == v.size() - 1 || y==v[0].size() - 1)
                    {
                        return count;
                    }
                    q.push({ x,y });
                    visited[x][y] = true;
                }
            }
        }
    }
    return -1;
}


int main()
{
    vector<vector<char>> maze = {{'+','+','+','+'}, {'.','.','.','.'}, {'+','+','+','+'}};
    vector<int>entrance = { 1, 0 };
    nearestExit(maze, entrance);
    return 0;
}